﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Microsoft.SqlServer.Types;
namespace SensorNetworkUI
{
    public class SensorStructure
    {
        int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
        double latesttemp;

        public double Latesttemp
        {
            get {

                
                string stmt = "select top 1 (sensor_reading) from reading where reading_date = (select top 1(reading_date) from reading where sensor_id = @sensor)";
                double latest = 0;
                SqlCommand comm = new SqlCommand(stmt, conn);
                comm.Parameters.Add("@sensor", id);
                conn.Open();
                SqlDataReader read = comm.ExecuteReader();
                if(read.Read())

                 latest= Double.Parse(read[0].ToString());
                conn.Close();


                return latest; }
            
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        
        SqlConnection conn = new SqlConnection(@"Data Source=CHIMBS-PC\CHIMBS;Initial Catalog=sensors;Integrated Security=True");
        private double _Longitude;

        public double Longitude
        {
            get { return _Longitude; }
            set { _Longitude = value; }
        }
        private double _Lattitude;

        public double Lattitude
        {
            get { return _Lattitude; }
            set { _Lattitude = value; }
        }
        public override string ToString()
        {
            return "Longitude: " + _Longitude + " Lattitude: " + _Lattitude; 
        }
        public void UpdateLocation()
        {


            SqlGeography geog = new SqlGeography();
            SqlGeographyBuilder bui = new SqlGeographyBuilder();


            bui.SetSrid(4326);
            bui.BeginGeography(OpenGisGeographyType.Point);
            bui.BeginFigure(this.Lattitude, this.Longitude);
            bui.EndFigure();
            bui.EndGeography();

            geog = bui.ConstructedGeography;


            string stmt = "update sensor set sensor_location = @db where sensor_id = @id";
            SqlCommand comm = conn.CreateCommand();
            comm.CommandText = stmt;
            SqlParameter par = new SqlParameter("@db", geog);
            par.UdtTypeName = "Geography";
            comm.Parameters.Add(par);
            comm.Parameters.AddWithValue("@id", this.id);

            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();

        }

        public bool fetch()
        {


            string stmt = "select * from sensor where sensor_id =@Id ";
            SqlCommand comm = conn.CreateCommand();
            comm.CommandText = stmt;
            comm.Parameters.AddWithValue("@ID", id);
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            while (dr.Read())
            {

               // SensorStructure st = new SensorStructure();
                SqlGeography geog = new SqlGeography();
                geog = (SqlGeography)dr["sensor_location"];
                this.Longitude = geog.Long.Value;
                this.Lattitude = geog.Lat.Value;
                this.Name = dr["sensor_name"].ToString();
                


            }
            conn.Close();

            return true;




        }
    }
}